package com.lu.pojo.reflection;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * @author luzhenfang
 * @date 2020 05 29
 * @description 通过反射获取注解信息 简单实现 ORM
 */

// 类名注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface TableName {
    String value();
}

// 字段注解
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface TableField {
    String columnName();

    String type();

    int length();
}

@TableName("db_fish")
class Fish {
    @TableField(columnName = "db_id", type = "int", length = 10)
    private int id;
    @TableField(columnName = "db_price", type = "int", length = 20)
    private int price;
    @TableField(columnName = "db_name", type = "varchar", length = 30)
    private String name;

    public Fish(int id, int price, String name) {
        this.id = id;
        this.price = price;
        this.name = name;
    }

    @Override
    public String toString() {
        return "Fish{" +
                "id=" + id +
                ", price=" + price +
                ", name='" + name + '\'' +
                '}';
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Fish() {
    }
}

// Bean对象
class FieldBean {
    private String fieldName;
    private String fieldType;
    private String fieldSize;

    public FieldBean(String fieldName, String fieldType, String fieldSize) {
        this.fieldName = fieldName;
        this.fieldType = fieldType;
        this.fieldSize = fieldSize;
    }


    public String getFieldName() {
        return fieldName;
    }

    public void setFieldName(String fieldName) {
        this.fieldName = fieldName;
    }

    public String getFieldType() {
        return fieldType;
    }

    public void setFieldType(String fieldType) {
        this.fieldType = fieldType;
    }

    public String getFieldSize() {
        return fieldSize;
    }

    public void setFieldSize(String fieldSize) {
        this.fieldSize = fieldSize;
    }

    @Override
    public String toString() {
        return "FieldBean{" +
                "fieldName='" + fieldName + '\'' +
                ", fieldType='" + fieldType + '\'' +
                ", fieldSize='" + fieldSize + '\'' +
                '}' + '\n';
    }
}

public class TestAnnotation {

    // 获取字段信息
    public static String getTableName(String className) {
        try {
            Class<?> aClass = Class.forName(className);
            TableName annotation = aClass.getAnnotation(TableName.class);
            return annotation.value();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    // 获取字段信息
    public static List<FieldBean> getFieldInfo() throws ClassNotFoundException, NoSuchFieldException {
        List<FieldBean> objects = new ArrayList<>();
        Class<?> aClass = Class.forName("com.lu.pojo.reflection.Fish");
        // 获取所有字段
        for (Field field : aClass.getDeclaredFields()) {
            // 获得类指定字段的注解
            TableField annotation = field.getAnnotation(TableField.class);
            objects.add(new FieldBean(annotation.columnName(), annotation.type(), String.valueOf(annotation.length())));
        }
        return objects;
    }
    // 生成创建表的SQL
    public static void makeCreateTableSQL(String dbName, List<FieldBean> field) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("CREATE TABLE");
        stringBuilder.append(" " + dbName + "(\n");
        for (FieldBean fieldBean : field) {
            stringBuilder.append(String.format("    %s %s (%s),\n", fieldBean.getFieldName(), fieldBean.getFieldType(), fieldBean.getFieldSize()));
        }
        stringBuilder.append(");");
        System.out.println(stringBuilder);
    }
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
        // 获取表名称
        String tableName = getTableName("com.lu.pojo.reflection.Fish");
        // 拿到字段信息
        List<FieldBean> objects = getFieldInfo();
        System.out.println(objects);
        // 生成SQL
        makeCreateTableSQL(tableName, objects);
    }
}
